/*
 * ActivePositionsPane.java
 *
 * Created on December 17, 2007, 11:28 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.fie.contentpanes;

import com.fie.database.Connector;
import com.fie.newsfeed.RssFeed;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author joseacuna-rohter
 */
public class ActivePositionsPane {
    
    private JButton addStockButton;
    private JButton removeStockButton;
    private JTextField inputStock;
    private JLabel tickerLabel;
    private JPanel totalGUI;
   
    private PositionsTablePane ptp;
    private int customerId;
    private Connector c;
    private RssFeed rf;
    private JLabel newsLabel;
    private BuySellPane bsp;
    
    /** Creates a new instance of ActivePositionsPane */
    public ActivePositionsPane(int customerId) {
        this.customerId = customerId;
        
    }
    
    
    public Container createContentPane()
    {
        
       
        // We create a bottom JPanel to place everything on.
        totalGUI = new JPanel();
        
        // We set the Layout Manager to null so we can manually place
        // the Panels.
        totalGUI.setLayout(null);
        
        // Now we create a new panel, size it, shape it,color it red.
        // then add it to the bottom JPanel.
        JPanel redPanel = new JPanel();
        redPanel.setBackground(Color.white);
        redPanel.setLocation(10, 10);
        redPanel.setSize(200,50);
        
        JButton findNewsButton = new JButton("Fetch News");
        
        findNewsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                actionAddPerformed(evt);
            }
        });
        
        redPanel.add(findNewsButton);
        
        totalGUI.add(redPanel);

        
        //Have the input text field here
        //AND also the tickerLabel
        JPanel textFieldPanel = new JPanel();
        textFieldPanel.setBackground(Color.white);
        textFieldPanel.setLocation(220,10);
        textFieldPanel.setSize(500,50);
        
        inputStock = new JTextField(5);
        inputStock.setHorizontalAlignment(0);
        tickerLabel = new JLabel("Symbol:");
        tickerLabel.setFont(new Font("arial",Font.BOLD,24));
        textFieldPanel.add(tickerLabel);
        textFieldPanel.add(inputStock);        
        totalGUI.add(textFieldPanel);
        
        
        //ADD THE TABLE HERE
        JPanel jTablePanel = new JPanel();
        jTablePanel.setBackground(Color.white);
        jTablePanel.setLocation(220,70);
        jTablePanel.setSize(500,160);
        
        ptp = new PositionsTablePane(customerId);
        jTablePanel.add(ptp.createContentPane());        
        totalGUI.add(jTablePanel);
        
        
         //ADD THE TABLE HERE
        JPanel removePane = new JPanel();
        removePane.setBackground(Color.white);
        removePane.setLocation(10,70);
        removePane.setSize(200,160);

        
        JPanel newsPane = new JPanel();
        newsPane.setBackground(Color.white);
        newsPane.setLocation(10,240);
        newsPane.setSize(700,250);
        newsPane.setLayout(new GridLayout(1,1));
        
        newsLabel = new JLabel("a");
        newsLabel.setFont(new Font("Arial",10,10));
        newsLabel.setSize(500,250);
        newsLabel.setLocation(10,10);
        newsLabel.setBackground(Color.red);
        newsLabel.setMaximumSize(new Dimension(800,650));
        
        
        
        RssFeed rf = new RssFeed();
        try{
            newsLabel.setText(rf.readRSSDocument());
          
        }catch(Exception e){
            e.printStackTrace();
        }
        
        newsPane.add(newsLabel);
        
        totalGUI.add(removePane);
        totalGUI.add(newsPane);
                
        // Finally we return the JPanel.
        totalGUI.setOpaque(true);
        
        return totalGUI;
    }
    
    
       /*
     *Action Performed for addStock button
     *
     */
    public void actionAddPerformed(ActionEvent e) {
               
       System.out.println("Add News");
       try{
           RssFeed rrf = new RssFeed();
           newsLabel.setSize(500,250);
           newsLabel.setBackground(Color.BLUE);
           newsLabel.setText(rrf.readRSSDocumentTicker(inputStock.getText().toUpperCase()));
           
           
           System.out.println(inputStock.getText().toUpperCase());
       }catch(Exception ex){
           ex.printStackTrace();
       }
        
        updateUI();    
    }
    
    
    public void actionRemovePerformed(ActionEvent e) {
        
        //
       
    }
    
    
    public void addPurchasedStock(String ticker){
        ptp.addRow(ticker);
    }
    
    public void removeSoldStock(String ticker){
        ptp.removeRow(ticker);
    }
    
    public void updateUI(){
        totalGUI.updateUI();
        
    }
    
}
